#include <nice2d/movingsprite.h>
#include <iostream>

class MovingSpriteData
{
public:
    sf::Vector2f velocity;
    float rotateVelocity = 0.0f;
    sf::Vector2f accelerate;
};

MovingSprite::MovingSprite():
    data(new MovingSpriteData)
{
}

MovingSprite::~MovingSprite()
{

}

void MovingSprite::setAcclerate(const sf::Vector2f &accelerate)
{
    data->accelerate = accelerate;
}

sf::Vector2f MovingSprite::getAccelerate() const
{
    return data->accelerate;
}

void MovingSprite::setVelocity(const sf::Vector2f &velocity)
{
    data->velocity = velocity;
}

sf::Vector2f MovingSprite::getVelocity()const
{
    return data->velocity;
}

void MovingSprite::setRotateVelocity(float velocity)
{
    data->rotateVelocity = velocity;
}

float MovingSprite::getRotateVelocity() const
{
    return data->rotateVelocity;
}

PTree MovingSprite::serialize()
{
    PTree node;
    SERIALIZE_BASE_OBJECT(node, Sprite)
    node.add_child("accelerate",serializeObject(getAccelerate()));
    node.add_child("velocity", serializeObject(getVelocity()));
    node.add("rotateVelocity", getRotateVelocity());
    return node;
}

void MovingSprite::deserialize(const PTree &node)
{
    DESERIALIZE_BASE_OBJECT(node, Sprite)
    data->accelerate = deserializeVector2f(node.get_child("accelerate"));
    data->velocity = deserializeVector2f(node.get_child("velocity"));
    data->rotateVelocity = node.get<float>("rotateVelocity");
}

void MovingSprite::onUpdateEntity(float deltaTime)
{
    sf::Vector2f delta(data->velocity * deltaTime);
    move(delta.x, delta.y);
    data->velocity += data->accelerate * deltaTime;
    float deltaRotate = data->rotateVelocity * deltaTime;
    setRotate(getRotate() + deltaRotate);

    onUpdateMovingSprite(deltaTime);
}

void MovingSprite::onUpdateMovingSprite(float deltaTime)
{
    (void)deltaTime;
}

